package com.kevis.zhunblog.elasticSearch.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kevis.zhunblog.core.entities.ArticleEntity;
import com.kevis.zhunblog.elasticSearch.dao.ArticleRepository;
import com.kevis.zhunblog.elasticSearch.entities.ArticleEs;
import com.kevis.zhunblog.service.ArticleService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@Api(tags = "增删改查（文档）")
@RestController
@RequestMapping("ariticle")
public class ArticleController {

    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    private ArticleService articleService;

    @GetMapping("add")
    public ArticleEs add() {
        //String now =LocalDateTime.now().plusMinutes(30).atOffset(ZoneOffset.ofHours(8)).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);

        ArticleEs entity = new ArticleEs();
        entity.setId(1);
        entity.setContent("这个是内容,报道最新的消息");
        entity.setArticlesPart("来自墨西哥鸡肉卷");
        entity.setAuthor("kevis");
        entity.setAdminId(3);
        entity.setClassId(1);
        entity.setSort(678);
        entity.setCreateTime(LocalDateTime.now());
        entity.setUpdateTime(LocalDateTime.now());
        entity.setStatus((byte) 1);
        entity.setNumClick(666);
        entity.setSummary("我的博客是一款华为手机");
        entity.setNumFavorite(999);
        entity.setTitle("这是我的凖博客");
        entity.setOpenComment((byte) 1);
        //entity.setReleaseTime(LocalDateTime.now());
        //entity.setReleaseTime(LocalDateTime.of(2023,10,15,15,23,35));
        //LocalDateTime t = LocalDateTime.of(2023,10,15,15,23,35);
        entity.setReleaseTime(Timestamp.valueOf(LocalDateTime.of(2023, 10, 15, 15, 23, 35)));
        return articleRepository.save(entity);
    }

    @GetMapping("get/{id}")
    public ArticleEs get(@PathVariable(name = "id") Integer id) {
        Optional<ArticleEs> optional = articleRepository.findById(id);
        ArticleEs entity = optional.get();
        return entity;
    }

    @PostMapping("addBath")
    public boolean addBath() {
        //todo:获取es最后更新时间,然后取这个之后的mysql数据
        int currPage = 1;
        //循环获取MySQL的数据
        Page<ArticleEntity> page = new Page<>(currPage, 1000);
        IPage<ArticleEntity> pageList = articleService.getPageList(null, page);
        if (pageList != null && pageList.getTotal() > 0) {
            writeEs(pageList.getRecords());
            if (page.getPages() > 1) {//表明有2页以上的数据,需要继续取
                while (page.getPages() >= currPage) {//表明取到了最后一页,那就跳出循环
                    currPage++;
                    page.setPages(currPage);
                    pageList = articleService.getPageList(null, page);
                    writeEs(pageList.getRecords());
                }
            }
        }
        //批量插入

        return true;
    }

    private void writeEs(List<ArticleEntity> originEntitys) {
        for (ArticleEntity originEntity : originEntitys) {
            ArticleEs entity = new ArticleEs();
            entity.setId(originEntity.getId());
            entity.setContent(originEntity.getContent());
            entity.setArticlesPart(originEntity.getArticlesPart());
            entity.setAuthor(originEntity.getAuthor());
            entity.setAdminId(originEntity.getAdminId());
            entity.setClassId(originEntity.getClassId());
            entity.setSort(originEntity.getSort());
            entity.setCreateTime(originEntity.getCreateTime());
            entity.setUpdateTime(originEntity.getUpdateTime());
            entity.setStatus(originEntity.getStatus());
            entity.setNumClick(originEntity.getNumClick());
            entity.setSummary(originEntity.getSummary());
            entity.setNumFavorite(originEntity.getNumFavorite());
            entity.setTitle(originEntity.getTitle());
            entity.setOpenComment(originEntity.getOpenComment());
            entity.setReleaseTime(originEntity.getReleaseTime());
            articleRepository.save(entity);
        }
    }
}
